﻿
@{
    Layout = "~/Views/Shared/_QueryMaster.cshtml";
}

@section styles
    {
    <style>
        .content {
            padding: 20px;
        }

        .panel-tool {
            line-height: 32px;
        }

        .panel-toolbar {
            float: right;
        }

        .panel-body {
            overflow: auto;
            height: calc(100% - 55px);
        }

        #choseUsers {
            height: calc(100% - 100px);
            height: -moz-calc(100% - 100px);
            height: -webkit-calc(100% - 100px);
        }

        .fixed-table-toolbar {
            display: none;
        }
    </style>
}

@section scripts
{
    <script>
        $(function() {
            loadTree();

            $("#btnAddOrg").click(function() {
                var treeObj = $.fn.zTree.getZTreeObj("orgTree");
                var parentId = "";
                var nodes = treeObj.getSelectedNodes();

                if (nodes.length > 0) {
                    parentId = nodes[0].id;
                }

                DiyWindow.modal({
                    id: "insertOrgUnit",
                    title: '新增机构',
                    width: 550,
                    height: 420,
                    showMaxBtn: false,
                    url: '@Url.Action("Create", "Organization")?parentId=' + parentId,
                    onClose: onLoadOrgNode
                });
            });

            $("#btnEditOrg").click(function() {
                var treeObj = $.fn.zTree.getZTreeObj("orgTree");
                var nodes = treeObj.getSelectedNodes();

                DiyWindow.modal({
                    id: "editOrgUnit",
                    title: '编辑机构',
                    width: 550,
                    height: 420,
                    showMaxBtn: false,
                    url: '@Url.Action("Edit", "Organization")?id=' + nodes[0].id,
                    onClose: onLoadOrgNode
                });
            });

            $("#btnEditGovOrg").click(function() {
                var treeObj = $.fn.zTree.getZTreeObj("orgTree");
                var nodes = treeObj.getSelectedNodes();

                DiyWindow.modal({
                    id: "editGovOrg",
                    title: '编辑部门政务',
                    width: 1000,
                    height: 520,
                    showMaxBtn: false,
                    url: '@Url.Action("OrgGovEdit", "Gov")?OrganizationId=' + nodes[0].id,
                });
            });

            $("#btnDeleteOrg").click(function() {
                var treeObj = $.fn.zTree.getZTreeObj("orgTree");
                var nodes = treeObj.getSelectedNodes();

                abp.message.confirm("您确认删除该机构吗？",
                    "删除机构",
                    function(isConfirm) {
                        if (isConfirm) {
                            abp.services.admin.organization.delete({ id: nodes[0].id }).done(function() {
                                abp.notify.success("机构删除成功！");
                                onLoadOrgNode();
                            });
                        }
                    });
            });


            $("#btnAddUser").click(function() {
                var treeObj = $.fn.zTree.getZTreeObj("orgTree");
                var nodes = treeObj.getSelectedNodes();


                var index = layer.open({
                    id: "choseUsers",
                    title: "选择用户",
                    type: 2,
                    content: "/Admin/Organization/SelectUsers?orgId=" + nodes[0].id,
                    area: ["90%", "80%"],
                    btn: ["确定", "关闭"],
                    btnclass: ["btn btn-primary", "btn btn-danger"],
                    yes: function(n, t) {
                        var selectRows = $(t).find("iframe")[0].contentWindow.getSelectRows();
                        if (selectRows.length > 0) {

                            var userIds = [];
                            $(selectRows).each(function() {
                                userIds.push(this.id);
                            });

                            abp.services.admin.user.setUserOrganization(nodes[0].id, userIds).done(function() {
                                abp.notify.success("用户添加成功");
                                onLoadOrgTable();
                            });

                            layer.close(index);

                        } else {
                            $(t).find("iframe")[0].contentWindow.abp.message.warn("请至少选择一个用户", "选择错误");
                        }
                    },
                    cancel: function() {
                        return !0;
                    }
                });
            });

            $("#btnDeleteUser").click(function() {
                var selectRows = onGetChoseUsers();

                if (selectRows != null) {
                    var userIds = [];
                    $(selectRows).each(function() {
                        userIds.push(this.id);
                    });

                    abp.message.confirm("您确认从该机构移除该用户吗？",
                        "移除用户",
                        function(isConfirm) {
                            if (isConfirm) {
                                abp.services.admin.user.removeOrganization(userIds).done(function() {
                                    abp.notify.success("用户移除成功！");
                                    onLoadOrgTable();
                                });
                            }
                        });
                }
            });
        });

        function loadTree() {
            $("#btnEditOrg").addClass("hidden");
            $("#btnDeleteOrg").addClass("hidden");
            $("#btnAddUser").addClass("hidden");
            $("#btnDeleteUser").addClass("hidden");
            $("#btnEditGovOrg").addClass("hidden");

            var setting2 = {
                view: {
                    showLine: true,
                    selectedMulti: true
                },
                data: {
                    key: {
                    },
                    simpleData: {
                        enable: true
                    }
                },
                callback: {
                    onClick: zTreeOnClick
                }
            }

            abp.ajax({
                url: '/api/services/admin/organization/GetOrganzationTree?isHasDepartment=true'
            }).done(function(data) {
                $.fn.zTree.init($("#orgTree"), setting2, data);
            });
        }

        function onLoadOrgNode() {
            loadTree();
            onLoadOrgTable();
        }

        function onLoadOrgTable() {
            var treeObj = $.fn.zTree.getZTreeObj("orgTree");
            var nodes = treeObj.getSelectedNodes();
            $("#OrgUnitId").val(nodes[0].id);
            onRefresh();
        }

        function zTreeOnClick(event, treeId, treeNode) {
            if (treeNode.type === 1) {
                $("#btnEditGovOrg").removeClass("hidden");
            } else {
                $("#btnEditGovOrg").addClass("hidden");
            }
            $("#btnEditOrg").removeClass("hidden");
            $("#btnDeleteOrg").removeClass("hidden");
            $("#btnAddUser").removeClass("hidden");
            $("#btnDeleteUser").removeClass("hidden");
            onLoadOrgTable();
        }
    </script>

    <script>
        $(function() {

            //1.初始化Table
            var oTable = new TableInit();
            oTable.Init();

            //2.初始化Button的点击事件
            var oButtonInit = new ButtonInit();
            oButtonInit.Init();

            $(window).resize(function() {
                $('#tb_users').bootstrapTable('resetView',
                    {
                        height: getTableHeight()
                    });
            });
        });

        var TableInit = function() {

            var oTableInit = new Object();
            //初始化Table
            oTableInit.Init = function() {
                $('#tb_users').bootstrapTable({
                    url: '/api/services/admin/user/GetOrganizationUsers', //请求后台的URL（*）
                    method: 'post', //请求方式（*）
                    toolbar: '#toolbar', //工具按钮用哪个容器
                    striped: true, //是否显示行间隔色
                    cache: false, //是否使用缓存，默认为true，所以一般情况下需要设置一下这个属性（*）
                    pagination: true, //是否显示分页（*）
                    sortable: false, //是否启用排序
                    sortOrder: "asc", //排序方式
                    queryParams: oTableInit.queryParams, //传递参数（*）
                    sidePagination: "server", //分页方式：client客户端分页，server服务端分页（*）
                    pageNumber: 1, //初始化加载第一页，默认第一页
                    pageSize: 20, //每页的记录行数（*）
                    pageList: [20, 25, 50, 100], //可供选择的每页的行数（*）
                    search: false, //是否显示表格搜索，此搜索是客户端搜索，不会进服务端，所以，个人感觉意义不大
                    strictSearch: true,
                    showColumns: false, //是否显示所有的列
                    showRefresh: false, //是否显示刷新按钮
                    minimumCountColumns: 2, //最少允许的列数
                    clickToSelect: true, //是否启用点击选中行
                    height: getTableHeight(), //行高，如果没有设置height属性，表格自动根据记录条数觉得表格高度
                    uniqueId: "id", //每一行的唯一标识，一般为主键列
                    showToggle: false, //是否显示详细视图和列表视图的切换按钮
                    cardView: false, //是否显示详细视图
                    detailView: false, //是否显示父子表
                    columns: [
                        {
                            checkbox: true
                        }, {
                            field: 'userName',
                            title: '账号'
                        }, {
                            field: 'name',
                            title: '名称'
                        }
                    ]
                });
            };

            //得到查询的参数
            oTableInit.queryParams = function(params) {
                var temp = { //这里的键的名字和控制器的变量名必须一直，这边改动，控制器也需要改成一样的
                    SearchCount: params.limit, //页面大小
                    SkipCount: params.offset, //页码
                    OrgUnitId: $("#OrgUnitId").val()
                };
                return temp;
            };
            return oTableInit;
        };

        var ButtonInit = function() {
            var oInit = new Object();
            var postdata = {};

            oInit.Init = function() {
                //初始化页面上面的按钮事件
            };

            return oInit;
        };


        function getTableHeight() {
            return $(".panel-body").height() + 20;
        }


        function onRefresh() {
            $('#tb_users').bootstrapTable('refresh');
        }

        function onGetChoseUsers() {
            var result = $('#tb_users').bootstrapTable('getSelections');

            if (result.length === 0) {
                abp.notify.error('请至少选择一行数据。');

                return null;
            }

            return result;
        }
    </script>
}

@Html.Hidden("OrgUnitId", 0)
<div class="col-sm-12 content full-height">
    <div class="col-sm-6 full-height">
        <div class="panel panel-default panel-tool full-height">
            <div class="panel-heading">
                <span class="panel-title">组织机构树</span>
                <div class="panel-toolbar">
                    <button id="btnAddOrg" action data-action-code='S0011_ADD' class="btn btn-success" data-toggle="modal" data-target="#myModal"><i class="fa fa-plus"></i> 新增</button>
                    <button id="btnEditOrg" action data-action-code='S0011_EDIT' class="btn btn-primary hidden"><i class="fa fa-pencil"></i> 编辑</button>
                    <button id="btnDeleteOrg" action data-action-code='S0011_DELETE' class="btn btn-danger hidden"><i class="fa fa-remove"></i> 删除</button>
                    <button id="btnEditGovOrg" action data-action-code='S0011_Depart' class="btn btn-primary hidden"><i class="fa fa-pencil"></i> 部门政务</button>
                </div>
            </div>
            <div class="panel-body">
                <ul id="orgTree" class="ztree"></ul>
            </div>
        </div>
    </div>

    <div class="col-sm-6 full-height">
        <div class="panel panel-default panel-tool full-height">
            <div class="panel-heading">
                <span class="panel-title">机构用户</span>

                <div class="panel-toolbar">
                    <button id="btnAddUser"  action data-action-code='S0011_Users' class="btn btn-success hidden"><i class="fa fa-user-plus"></i> 添加用户</button>
                    <button id="btnDeleteUser"  action data-action-code='S0011_Users' class="btn btn-danger hidden"><i class="fa fa-user-times"></i> 移除用户</button>
                </div>
            </div>
            <div class="panel-body">
                <table id="tb_users"></table>
            </div>
        </div>
    </div>
</div>
